package org.example.demo_xiaowuz.controller;

import jakarta.servlet.http.HttpSession;
import org.example.demo_xiaowuz.model.Result;
import org.example.demo_xiaowuz.model.UserInfo;
import org.example.demo_xiaowuz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping(value = "/login", produces = "application/json")
    public Result login(String userName, String password, HttpSession session) {
        if(!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)) {
            return Result.fail("用户名或密码不能为空");
        }
        UserInfo userInfo=userService.getByUserName(userName);
        if(userInfo==null) {
            return Result.fail("用户不存在");
        }
        if(!password.equals(userInfo.getPassword())) {
            return Result.fail("密码错误");
        }
        userInfo.setPassword("");
        session.setAttribute("session_user_key",userInfo);
        return Result.success(userInfo);
    }
}
